package where2go.sctu.com.zhiyou.fragment;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.treeview_lib.TreeNode;
import where2go.sctu.com.treeview_lib.TreeView;
import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.db.MyNodeViewFactory;
import where2go.sctu.com.zhiyou.entity.LinePlan;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.fragment
 * Created by 张某人
 * CreatedTime:on 2018/5/8   00:34
 * Description: 线路规划
 */

public class LinePlanFragment extends Fragment {

    private ViewGroup viewGroup;
    private TreeNode root;
    private TreeView treeView;

    private List<LinePlan.DataBean> lineList=new ArrayList<LinePlan.DataBean>();
    private List<LinePlan.DataBean.DetailBean> mList=new ArrayList<LinePlan.DataBean.DetailBean>();

    private List<String>  lineScenicName=new ArrayList<String>();
    private List<String>  lineTitle=new ArrayList<String>();
    private List<String>  lineScenicDetail=new ArrayList<String>();

    private String classId="";
    private String url="http://47.104.80.155/youyiyou/line/allinfo?classId=";


    private static LinePlanFragment fa;

    public static LinePlanFragment getLinePlanFragment() {
        if (fa == null) {
            fa = new LinePlanFragment();
        }
        return fa;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.line_plan_fragment, null);

        initView(view);
        classId=getArguments().getString("id");
        root = TreeNode.root();
        getLinePlan(url+classId);
        return view;
    }



    private void initView(View view) {
        viewGroup = (RelativeLayout) view.findViewById(R.id.line_container);
        setLightStatusBar(viewGroup);
    }


    private void setLightStatusBar(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int flags = view.getSystemUiVisibility();
            getActivity().getWindow().setStatusBarColor(Color.WHITE);
            flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            view.setSystemUiVisibility(flags);
        }
    }

    /**
     * 网络请求，获取线路规划
     * @param url
     */
    private void getLinePlan(String url){
        root = TreeNode.root();
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                LinePlan linePlan=gson.fromJson(response,LinePlan.class);
                lineList=linePlan.getData();
                for (int i = 0; i <lineList.size() ; i++) {
                    lineTitle.add(lineList.get(i).getLineTitle());
                    for(int j=0;j<lineList.get(i).getDetail().size();j++){
                    lineScenicDetail.add(lineList.get(i).getDetail().get(j).getLineScenicDetail());
                    lineScenicName.add(lineList.get(i).getDetail().get(j).getLineScenicName());
                    }
                }
                TreeNode treeNode;
                TreeNode treeNode1;
                TreeNode treeNode2;
                for (int m = 0; m < lineTitle.size(); m++) {
                     treeNode = new TreeNode(new String(lineTitle.get(m)));
                    treeNode.setLevel(0);
                    for (int n = 0; n< lineScenicName.size(); n++) {
                         treeNode1 = new TreeNode(new String(lineScenicName.get(n)));
                        treeNode1.setLevel(1);
                        for (int k = 0; k <lineScenicDetail.size(); k++) {
                            treeNode2 = new TreeNode(new String(lineScenicDetail.get(k)));
                            treeNode2.setLevel(2);
                            treeNode1.addChild(treeNode2);
                        }
                        treeNode.addChild(treeNode1);
                    }
                    root.addChild(treeNode);
                }
                treeView = new TreeView(root, getActivity(), new MyNodeViewFactory());
                View mView = treeView.getView();
                mView.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                viewGroup.addView(mView);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.toString());
                Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
                 }
        });
        request.setTag("getLinePlan");
        MyApplication.getHttpQueues().add(request);
    }

    @Override
    public void onPause() {
        MyApplication.getHttpQueues().cancelAll("getLinePlan");
        super.onPause();
    }

    @Override
    public void onDestroy() {
        MyApplication.getHttpQueues().cancelAll("getLinePlan");
        super.onDestroy();
    }
}
